﻿namespace NEvoWeb.Modules.NB_Store
{
    using DotNetNuke.Common;
    using DotNetNuke.Entities.Portals;
    using DotNetNuke.Services.Localization;
    using Microsoft.VisualBasic.CompilerServices;
    using System;
    using System.Collections;
    using System.Runtime.CompilerServices;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public class SelectLang : UserControl
    {
        private bool _DisplayNoLang;
        [AccessedThroughProperty("dlLanguages")]
        private DataList _dlLanguages;
        private int _RepeatColumns;
        private string _SelectedFlagUrl;
        private bool _Visible;
        private PortalSettings PS;

        public event AfterChangeEventHandler AfterChange;

        public event BeforeChangeEventHandler BeforeChange;

        public event ItemCommandEventHandler ItemCommand;

        public SelectLang()
        {
            base.Load += new EventHandler(this.Page_Load);
            base.Init += new EventHandler(this.Page_Init);
            this._RepeatColumns = 0;
            this._DisplayNoLang = false;
            this._Visible = false;
        }

        private void buildMenu()
        {
            LocaleCollection validLocales = SharedFunctions.GetValidLocales();
            if ((validLocales.Count > 1) | this._DisplayNoLang)
            {
                LocaleCollection locales2 = new LocaleCollection();
                if (this._DisplayNoLang)
                {
                    locales2.Add("None", "None");
                    int num2 = validLocales.Count - 1;
                    for (int i = 0; i <= num2; i++)
                    {
                        DictionaryEntry entry = validLocales[i];
                        DictionaryEntry entry2 = validLocales[i];
                        locales2.Add(Conversions.ToString(entry.Key), RuntimeHelpers.GetObjectValue(entry2.Value));
                    }
                }
                else
                {
                    locales2 = validLocales;
                }
                if (this._RepeatColumns > 0)
                {
                    this.dlLanguages.RepeatColumns = this._RepeatColumns;
                }
                this._Visible = true;
                this.dlLanguages.DataSource = locales2;
                this.dlLanguages.DataBind();
            }
            else
            {
                SharedFunctions.setStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture", "None", 1.0);
                this.dlLanguages.Visible = false;
                this._Visible = false;
            }
        }

        private void dlLanguages_ItemCommand(object source, DataListCommandEventArgs e)
        {
            DataListItem item = e.Item;
            string previousEditLang = "";
            if (e.CommandName == "Change")
            {
                if (this.Page.IsValid)
                {
                    HiddenField field = (HiddenField) e.Item.FindControl("hidCultureCode");
                    if (field != null)
                    {
                        
                        if (this.BeforeChange != null)
                        {
                            BeforeChange(RuntimeHelpers.GetObjectValue(source), e, field.Value);
                        }
                        previousEditLang = SharedFunctions.getStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture");
                        SharedFunctions.setStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture", field.Value, 1.0);
                        this.buildMenu();
                        
                        if (this.AfterChange != null)
                        {
                            AfterChange(RuntimeHelpers.GetObjectValue(source), e, previousEditLang);
                        }
                    }
                }
            }
            else
            {
                
                if (this.ItemCommand != null)
                {
                    ItemCommand(RuntimeHelpers.GetObjectValue(source), e);
                }
            }
        }

        private void dlLanguages_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem))
            {
                LinkButton button = (LinkButton) e.Item.FindControl("cmdFlagLang");
                HiddenField field = (HiddenField) e.Item.FindControl("hidCultureCode");
                string str = SharedFunctions.getStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture");
                if (button != null)
                {
                    field.Value = e.Item.DataItem.ToString();
                    button.ToolTip = e.Item.DataItem.ToString();
                    if (e.Item.DataItem.ToString() == str)
                    {
                        if (field.Value == "None")
                        {
                            button.Text = "<div style=\"padding:1px;border:#ABABAB solid 1px;\"><div style=\"border:0px;width: 18px;height:12px;background:#dddddd;\"></div></div>";
                        }
                        else
                        {
                            this._SelectedFlagUrl = Globals.ResolveUrl("~/images/flags/" + e.Item.DataItem.ToString() + ".gif");
                            button.Text = "<div style=\"padding:1px;border:#ABABAB solid 1px;\"><img src=\"" + this._SelectedFlagUrl + "\"  height=\"12\" width=\"18\" border=\"0\"/></div>";
                        }
                    }
                    else if (field.Value == "None")
                    {
                        button.Text = "<div style=\"padding:1px;border:white solid 1px;\"><div style=\"border:0px;width: 18px;height:12px;background:#dddddd;\"></div></div>";
                    }
                    else
                    {
                        button.Text = "<div style=\"padding:1px;border:white solid 1px;\"><img src=\"" + Globals.ResolveUrl("~/images/flags/" + e.Item.DataItem.ToString() + ".gif") + "\" height=\"12\" width=\"18\" border=\"0\" /></div>";
                    }
                }
            }
        }

        private void Page_Init(object sender, EventArgs e)
        {
            this.PS = PortalController.GetCurrentPortalSettings();
            if (SharedFunctions.getStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture") == "")
            {
                SharedFunctions.setStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture", "None", 1.0);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                this.buildMenu();
            }
        }

        public void Refresh()
        {
            this.buildMenu();
        }

        public bool DisplayNoLang
        {
            get
            {
                return this._DisplayNoLang;
            }
            set
            {
                this._DisplayNoLang = value;
            }
        }

        protected virtual DataList dlLanguages
        {
            get
            {
                return this._dlLanguages;
            }
            [MethodImpl(MethodImplOptions.Synchronized)]
            set
            {
                DataListCommandEventHandler handler = new DataListCommandEventHandler(this.dlLanguages_ItemCommand);
                DataListItemEventHandler handler2 = new DataListItemEventHandler(this.dlLanguages_ItemDataBound);
                if (this._dlLanguages != null)
                {
                    this._dlLanguages.ItemCommand -= handler;
                    this._dlLanguages.ItemDataBound -= handler2;
                }
                this._dlLanguages = value;
                if (this._dlLanguages != null)
                {
                    this._dlLanguages.ItemCommand += handler;
                    this._dlLanguages.ItemDataBound += handler2;
                }
            }
        }

        public string EditedLang
        {
            get
            {
                return SharedFunctions.getStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminEditedCulture");
            }
            set
            {
                SharedFunctions.setStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminEditedCulture", value, 1.0);
            }
        }

        public int RepeatColumns
        {
            get
            {
                return this._RepeatColumns;
            }
            set
            {
                this._RepeatColumns = value;
            }
        }

        public string SelectedFlagUrl
        {
            get
            {
                return this._SelectedFlagUrl;
            }
            set
            {
                this._SelectedFlagUrl = value;
            }
        }

        public string SelectedLang
        {
            get
            {
                string str = SharedFunctions.getStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture");
                if (!this._DisplayNoLang & (str == "None"))
                {
                    return SharedFunctions.GetCurrentCulture();
                }
                return str;
            }
            set
            {
                SharedFunctions.setStoreCookieValue(this.PS.PortalId, "AdminLang", "AdminSelectedCulture", value, 1.0);
            }
        }

        public delegate void AfterChangeEventHandler(object source, DataListCommandEventArgs e, string PreviousEditLang);

        public delegate void BeforeChangeEventHandler(object source, DataListCommandEventArgs e, string NewEditLang);

        public delegate void ItemCommandEventHandler(object source, DataListCommandEventArgs e);
    }
}

